package com.demo.mysql.config;

import com.demo.mysql.base.MultipleMybatisConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hushengdong
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.demo.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class MyBatisConfig extends MultipleMybatisConfig {

    public final static String DB01 = "m1";
    public final static String DB02 = "m2";

    @Override
    protected String getTypeAliasesPackage() {
        return "com.demo.model";
    }

    @Override
    protected Map<Object, Object> getAllDataSource() throws Exception {
        Map<Object, Object> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put(DB01, getDataSource(DB01));
        dataSourceMap.put(DB02, getDataSource(DB02));
        return dataSourceMap;
    }

    @Override
    protected Object getDefaultTargetDataSource() throws Exception {
        return getDataSource(DB01);
    }

    public DataSourceTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(multipleDataSource());
    }

    @Override
    protected String mapperLocations() {
        return "classpath:mappers/*.xml";
    }

//    @Override
//    protected Interceptor[] getPlugins() {
//        //分页插件
//        PageHelper pageHelper = new PageHelper();
//        Properties properties = new Properties();
//        //false如果所选页没有值返回空，true返回最后一页或者第一页
//        properties.setProperty("reasonable", "false");
//        //传入参数有pageNum和pageSize自动分页
//        properties.setProperty("supportMethodsArguments", "false");
//        //多数据源时应该为true
//        properties.setProperty("autoRuntimeDialect", "true");
//        pageHelper.setProperties(properties);
//        //添加插件
//        return new Interceptor[]{pageHelper};
//    }

}
